package com.exercise.widgets.menu

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ContextMenu
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView.AdapterContextMenuInfo
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivityContextMenuBinding
import com.exercise.widgets.menu.adapter.PersonAdapter
import com.exercise.widgets.menu.model.Person

class ContextMenuActivity : BaseActivity<BaseViewModel, ActivityContextMenuBinding>() {
    private val mAdapter by lazy { PersonAdapter() }

    override val bindingInflater: (LayoutInflater) -> ActivityContextMenuBinding
        get() = ActivityContextMenuBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
        val data = (1..10).map { Person("Item:$it") }
        mAdapter.setList(data)
    }

    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
        menuInflater.inflate(R.menu.menu_simple_list, menu)
    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        val info = item.menuInfo as AdapterContextMenuInfo
        return when (item.itemId) {
            R.id.edit -> {
                Toast.makeText(this, "Edit: ${info.position}", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.delete -> {
                Toast.makeText(this, "Delete: ${info.position}", Toast.LENGTH_SHORT).show()
                true
            }

            else -> super.onContextItemSelected(item)
        }
    }

    override fun initView() {
        registerForContextMenu(binding.recyclerView)
        mAdapter.run {
            addChildClickViewIds(R.id.editBtn, R.id.deleteBtn)
            setOnItemChildClickListener { _, view, position ->
                val item = mAdapter.getItem(position)
                when (view.id) {
                    R.id.editBtn -> {
                        Toast.makeText(
                            this@ContextMenuActivity,
                            "edit: ${item.title}",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                    R.id.deleteBtn -> {
                        Toast.makeText(
                            this@ContextMenuActivity,
                            "delete: ${item.title}",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                    else -> {}
                }
            }
            setOnItemClickListener { _, _, position ->
                val item = mAdapter.getItem(position)
                Toast.makeText(
                    this@ContextMenuActivity,
                    "ItemClick: ${item.title}",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
        binding.recyclerView.adapter = mAdapter
    }

    override fun onDestroy() {
        unregisterForContextMenu(binding.recyclerView)
        super.onDestroy()
    }

    companion object {
        fun start(context: Context) {
            Intent(context, ContextMenuActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}